package hu.arguscab.ai.game;

import hu.arguscab.ai.FitnessFunction;
import hu.arguscab.ai.Model;
import hu.arguscab.game.WorldState;
import hu.arguscab.physic.Vector;

/**
 * Returns with the distance of the ship and its target.
 * @author sharp
 */
public class FDistance extends FitnessFunction {

	private int max;

	public FDistance() {	
		//int h = GameInfo.instance().getMap().getHeight();
		//int w = GameInfo.instance().getMap().getWidth();
		//max = Math.max(h, w);
		max = 1024;
	}

	@Override
	public float getMinValue() {
		return 0;
	}

	@Override
	public float getMaxValue() {
		return max;
	}

	@Override
	public float calcF(Model m) {
		WorldState state = (WorldState)m.getState();
		Vector myPos = state.getShipState().getPos();
		Vector targetPos = ((Bot)state.getShipState().getShip().getController()).getTarget().getState().getPos();
		float x = Math.abs( targetPos.getX() - myPos.getX() );
		float y = Math.abs( targetPos.getY() - myPos.getY() );
		float d = Math.max(x, y);
		return Math.max(0, max - d);
	}

	@Override
	public String toString() {
		return "FDistance";
	}

}
